package com.example.sdbtspringboot.controller;

import cn.hutool.core.util.StrUtil;
import com.example.sdbtspringboot.common.AuthAccess;
import com.example.sdbtspringboot.common.Result;
import com.example.sdbtspringboot.entity.User;
import com.example.sdbtspringboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.security.util.Length;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;


@RestController
public class WebController {
    @Resource
    UserService userService;



    @AuthAccess
    @GetMapping("/")
    public Result hello(){
        return Result.success("success");
    }


    @AuthAccess
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        if (StrUtil.isBlank(user.getUsername())||StrUtil.isBlank(user.getPassword())){
            return Result.error("数据输入不合法");
        }
            user = userService.login(user);
        return Result.success(user);
    }
    @AuthAccess
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        if (StrUtil.isBlank(user.getUsername())||StrUtil.isBlank(user.getPassword())||StrUtil.isBlank(user.getRole())){
            return Result.error("数据输入不合法");
        }
        if (user.getUsername().length()>10||user.getPassword().length()>20){
            return Result.error("数据输入不合法");
        }
        user = userService.register(user);
        return Result.success(user);
    }
/*
* 重置密码
* */
    @AuthAccess
    @PutMapping("/password")
    public Result password(@RequestBody User user){
        if (StrUtil.isBlank(user.getUsername())||StrUtil.isBlank(user.getPhone())){
            return Result.error("数据输入不合法");
        }
        userService.resetPassword(user);
        return Result.success(user);
    }

//    @PutMapping("/put")
////    @PutMapping("/put")
//    public Result put(@RequestBody Obj obj){
////        System.out.println("hello 帅的不谈!");
//        return Result.success(obj);
//    }
//
//    @DeleteMapping("/delete/{id}")
//    public Result delete(@PathVariable Integer id){
////        System.out.println("hello 帅的不谈!");
//        return Result.success(id);
//    }
}
